﻿using System;
using System.Web;

namespace Facebook.Web
{
    [Serializable]
    public class FacebookWebRequest
    {
        public static Boolean IsReady
        {
            get { return (Boolean)(HttpContext.Current.Items["__FBREADY"] ?? false); }
            internal set { HttpContext.Current.Items["__FBREADY"] = value; }
        }

        public static FacebookWebRequest Current
        {
            get
            {
                if (HttpContext.Current.Items["__FBREQUEST"] == null && HttpContext.Current.Session["__FBREQUEST"] != null)
                {
                    HttpContext.Current.Items["__FBREQUEST"] = HttpContext.Current.Session["__FBREQUEST"];
                }
                return (FacebookWebRequest)HttpContext.Current.Items["__FBREQUEST"];
            }
            internal set
            {
                HttpContext.Current.Items["__FBREQUEST"] = value;
                HttpContext.Current.Session["__FBREQUEST"] = value;
            }
        }

        public static void Init(HttpContext context)
        {
            if (FacebookWebRequest.Current == null) FacebookWebRequest.Current = new FacebookWebRequest(context);
            else FacebookWebRequest.Current.Merge(context);
        }

        public FacebookWebRequest(HttpContext context)
        {
            this.Merge(context);
        }

        public void Merge(HttpContext context)
        {
            Boolean inCanvas;
            if (Boolean.TryParse(context.Request.Params["fb_sig_in_canvas"], out inCanvas)) this.InCanvas = inCanvas;

            Boolean inProfileTab;
            if (Boolean.TryParse(context.Request.Params["fb_sig_in_profile_tab"], out inProfileTab)) this.InProfileTab = inProfileTab;

            Int64 profileUserId;
            if (Int64.TryParse(context.Request.Params["fb_sig_profile_user"], out profileUserId)) this.ProfileUserId = profileUserId;

            String profileSessionKey = context.Request.Params["fb_sig_profile_session_key"];
            if (!String.IsNullOrEmpty(profileSessionKey)) this.ProfileSessionKey = profileSessionKey;

            Int64 pageId;
            if (Int64.TryParse(context.Request.Params["fb_sig_page_id"], out pageId)) this.PageId = pageId;

            Boolean isPageAdded;
            if (Boolean.TryParse(context.Request.Params["fb_sig_page_added"], out isPageAdded)) this.IsPageAdded = isPageAdded;

            Boolean isLoggedOutOfFacebook;
            if (Boolean.TryParse(context.Request.Params["fb_sig_logged_out_facebook"], out isLoggedOutOfFacebook)) this.IsLoggedOutOfFacebook = isLoggedOutOfFacebook;

            Int64 uid;
            if (!Int64.TryParse(context.Request.Params["fb_sig_user"], out uid))
                if (!Int64.TryParse(context.Request.Params["fb_sig_canvas_user"], out uid))
                    Int64.TryParse(context.Request.Params["fb_sig_profile_user"], out uid);
            if (uid > 0) this.Uid = uid;
        }

        public Boolean InCanvas { get; set; }
        public Boolean InProfileTab { get; set; }
        public Int64 ProfileUserId { get; set; }
        public String ProfileSessionKey { get; set; }
        public Int64 PageId { get; set; }
        public Boolean IsPageAdded { get; set; }
        public Boolean IsLoggedOutOfFacebook { get; set; }
        public Int64 Uid { get; set; }
    }
}
